package com.company.stack;

import java.util.Stack;

public class BaseBallGame {


    private Stack<String> optionStack = new Stack<>();

    public int calPoints(String[] ops) {
        for (String action : ops) {
            switch (action) {
                case "+" -> optionPlus();
                case "D" -> optionD();
                case "C" -> optionC();
                //将得分压入栈
                default -> optionX(action);
            }
        }
        return getResult();
    }

    private int getResult(){
        int result=0;
        while (!optionStack.isEmpty()){
            String value = optionStack.pop();
            int v = Integer.parseInt(value);
            result+=v;

        }
        return result;
    }

    private void optionPlus() {
        String score1 = optionStack.pop();
        String score2 = optionStack.pop();
        int s1 = Integer.parseInt(score1);
        int s2 = Integer.parseInt(score2);

        optionStack.push(score2);
        optionStack.push(score1);
        optionStack.push((s1+s2)+"");
    }

    private void optionD() {
        String score = optionStack.pop();
        int s = Integer.parseInt(score);
        optionStack.push(score);
        optionStack.push((s<<1)+"");
    }

    private void optionC() {
        optionStack.pop();
    }

    private void optionX(String x) {
        optionStack.push(x);
    }

}
